package com.shine.demo.controller;

import com.shine.demo.common.result.Result;
import com.shine.demo.entity.SysUser;
import com.shine.demo.service.IUserService;
import com.shine.demo.utils.PdfUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/test")
public class TestController {

    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @Resource
    IUserService userService;

    @PostMapping("/loadUserByUsername")
    @ResponseBody
    public Result loadUserByUsername(String name){
        return userService.getUserName(name);
    }

    @PostMapping("/register")
    @ResponseBody
    public Result loadUserByUsername(SysUser user){
        return  userService.register(user);
    }





    @RequestMapping("/generate")
    public void generatePDF(HttpServletResponse response) throws Exception {
        String filename = "测试pdf";
        // 设置下载格式为pdf
        response.setContentType("application/x-download");
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".pdf");
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        // 1.
        Document document = PdfUtil.createDocument();
        document.setPageSize(PageSize.A4);
        // 2. 获取writer
        PdfWriter.getInstance(document, os);
        // 3. open()
        document.open();

        //设置字体
        Font blackFont = PdfUtil.createFont(10, Font.NORMAL, BaseColor.BLACK);
        Font blueFont = PdfUtil.createFont(10, Font.NORMAL, BaseColor.BLUE);
        Font bigFont = PdfUtil.createFont(14, Font.NORMAL, BaseColor.BLACK);
        Font littleFont = PdfUtil.createFont(8, Font.NORMAL, BaseColor.BLACK);

        Paragraph title = PdfUtil.createParagraph("天府银行授信业务档案目录", bigFont);
        title.setAlignment(Element.ALIGN_CENTER);
        // 4. 添加段落内容
        document.add(title);
        //目录表格
        PdfPTable table  = PdfUtil.createPdfPTable(6);
        table.setWidths(new int[] { 1, 2, 1, 2,1,2});
        PdfPCell cell;
        table.addCell(new PdfPCell(new Phrase("客户名称", littleFont)));
        table.addCell(new Phrase("", littleFont));

        table.addCell(new PdfPCell(new Phrase("业务品种", littleFont)));
        table.addCell(new Phrase("", littleFont));

        table.addCell(new PdfPCell(new Phrase("金额", littleFont)));
        table.addCell(new Phrase("", littleFont));

        table.addCell(new PdfPCell(new Phrase("事业部", littleFont)));
        table.addCell(new Phrase("", littleFont));

        table.addCell(new PdfPCell(new Phrase("客户经理组", littleFont)));
        table.addCell(new Phrase("", littleFont));

        table.addCell(new PdfPCell(new Phrase("客户经理组A1", littleFont)));
        table.addCell(new Phrase("", littleFont));
        // 由六列变成两列
        cell = new PdfPCell(new Phrase("内容",littleFont));
        cell.setColspan(5);//合并列
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
        table.addCell(cell);
        table.addCell( new PdfPCell(new Phrase("页码",littleFont)));

        cell = new PdfPCell(new Phrase("授信申请人资料",littleFont));
        cell.setRowspan(4);//合并行
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("企业证件资料",littleFont));
        cell.setRowspan(2);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("营业执照复印件",littleFont));
        cell.setColspan(3);
        table.addCell(cell);
        table.addCell(new PdfPCell(new Phrase("1", littleFont)));
        cell = new PdfPCell(new Phrase("税务登记证复印件",littleFont));
        cell.setColspan(3);
        table.addCell(cell);
        table.addCell(new PdfPCell(new Phrase("2", littleFont)));

        cell = new PdfPCell(new Phrase("企业主要负责人证件资料",littleFont));
        cell.setRowspan(2);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("法人代表身份证复印件",littleFont));
        cell.setColspan(3);
        table.addCell(cell);
        table.addCell(new PdfPCell(new Phrase("3", littleFont)));
        cell = new PdfPCell(new Phrase("实际控制人身份证复印件",littleFont));
        cell.setColspan(3);
        table.addCell(cell);
        table.addCell(new PdfPCell(new Phrase("4", littleFont)));


        document.add(table );
        // 5. close()
        document.close();
        os.close();
    }

    @RequestMapping("/test")
    public Result generatePDF() throws Exception {
        return Result.failed();
    }

}
